﻿using EFCore.BulkExtensions;
using EFCoreDemo.Database;
using EFCoreDemo.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace EFCoreDemo.Repository.BulkExtensions
{
    public class BulkExtensionRepository<T> : BaseRepository<T> where T : class, IEntity
    {
        public BulkExtensionRepository(DatabaseContext context) : base(context)
        {

        }
        public override List<T> AddRange(List<T> entitys)
        {
            //return base.AddRange(entitys);
            foreach (var entity in entitys)
            {
                if (entity.Id == 0)
                {
                    entity.Id = IdGenerate.NetLongId();
                }
            }
            var propertiesToExclude = new List<string>();
            if (propertiesToExclude == null)
            {
                propertiesToExclude = new List<string>();
            }
            if (typeof(IHasSoftDelete).IsAssignableFrom(typeof(T)))
            {
                propertiesToExclude.Add("IsDeleted");
            }
            context.BulkInsert(entitys);
            //context.BulkInsert(entitys, new BulkConfig
            //{
            //    PropertiesToExclude = propertiesToExclude
            //});
            return entitys;
        }
        public override int DeleteRange(Expression<Func<T, bool>> predicate)
        {
            //if (typeof(IHasSoftDelete).IsAssignableFrom(typeof(T)))
            //{
                return context.Set<T>().Where(predicate).BatchUpdate(new { UserName = "xuzhongye" });
            //}
            //else
            //{
            //    return context.Set<T>().Where(predicate).BatchDelete();
            //}
        }
    }
}
